package com.tjetc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class WebClientConfig {

    @Bean
    public WebClient webClient() {
        return WebClient.builder().build();
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

        // 允许的域名，可以使用 * 表示允许所有域名
        config.addAllowedOrigin("*");
        // 或者指定域名:
        // config.addAllowedOrigin("http://localhost:8080");
        // config.addAllowedOrigin("https://yourdomain.com");

        // 允许的HTTP方法
        config.addAllowedMethod("*");

        // 允许的请求头
        config.addAllowedHeader("*");

        // 是否允许凭证
        config.setAllowCredentials(false);

        // 预检请求的有效期，单位为秒
        config.setMaxAge(3600L);

        // 对所有路径应用这些CORS配置
        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);
    }
}